<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.keyword" placeholder="关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="getTableData" icon="el-icon-search" size="small">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-plus" size="small" @click="add">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="tableData" highlight-current-row v-loading="listLoading" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="name" label="标题">
      </el-table-column>
      <el-table-column prop="intro" label="简介">
      </el-table-column>
      <el-table-column prop="state" label="状态" width="100" :formatter="formatState" sortable>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="edit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="del(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="20" :total="total"
                     style="float:right;">
      </el-pagination>
    </el-col>

    <el-dialog :title="title" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <el-form :model="saveForm" label-width="80px" ref="blacklistForm">
        <el-form-item label="名称" prop="name">
          <el-input v-model="saveForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="类型">
          <el-select v-model="saveForm.parentId" value-key="id" placeholder="请选择" clearable>
            <el-option v-for="item in systemDictionary"
                       :label="item.name"
                       :value="item.id">
              <span style="float: left">{{ item.name }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.sn }}
            </span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="描述" prop="intro">
          <el-input v-model="saveForm.intro" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="saveForm.state">
            <el-radio class="radio" :label="0">启用</el-radio>
            <el-radio class="radio" :label="1">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit" :loading="listLoading">提交</el-button>
      </div>
    </el-dialog>


  </section>
</template>

<script>

export default {
  data() {
    return {
      filters: {
        keyword: ''
      },
      tableData: [],
      total: 0,
      page: 1,
      listLoading: false,
      saveFormVisible: false,
      sels: [],
      title: '',
      saveForm: {
        id: null,
        parentId: null,
        name: '',
        intro: '',
      },
      systemDictionary: [],
    }

  },
  methods: {
    //性别显示转换
    formatState: function (row, column) {
      return row.sex == 1 ? '禁用' : '启用';
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getTableData();
    },
    //获取用户列表
    getTableData() {
      this.listLoading = true;
      let para = {
        page: this.page,
        keyword: this.filters.keyword
      };
      this.$http.post("/system/systemdictionaryitem/pagelist", para).then(result => {
        let {success, data, message, code} = result.data;
        if (success) {
          this.total = data.total;
          this.tableData = data.rows;
        } else {
          this.$message({message: "数据加载失败[" + message + "]", type: 'error'});
        }
        this.listLoading = false;
      }).catch(error => {
        this.listLoading = false;
        this.$message({message: "数据加载失败[" + error.message + "]", type: 'error'});
      })
    },
    add() {
      this.title = "添加数据字典明细";
      this.getSystemDictionary();
      this.saveForm = {
        id: null,
        parentId: null,
        name: '',
        intro: '',
      }
      this.saveFormVisible = true;
    },
    saveSubmit() {
      let parentId = this.saveForm.parentId
      if (parentId && parentId.length == 0) {
        this.saveForm.parentId = null;
      }
      if (parentId && parentId.length > 0) {
        this.saveForm.parentId = parentId[parentId.length - 1]
      }
      this.$confirm('确认提交吗？', '提示', {}).then(() => {
        this.saveFormVisible = false
        this.$http.post("/system/systemdictionaryitem/save", this.saveForm).then(result => {
          this.listLoading = true;
          let data = result.data;
          if (data.success) {
            this.$message.success("数据字典明细添加成功")
            this.getTableData();
          }
          this.listLoading = false;
        }).catch(error => {
          this.listLoading = false;
          this.$message({message: "数据字典明细添加失败[" + error.message + "]", type: 'error'});
        });
      })
    },
    del(index,row) {
      this.$http.delete("/system/systemdictionaryitem/"+row.id).then(result => {
        this.listLoading = true;
        let data = result.data;
        if (data.success) {
          this.$message.success("数据字典明细删除成功")
          this.getTableData();
        }
        this.listLoading = false;
      }).catch(error => {
        this.listLoading = false;
        this.$message({message: "数据字典明删除失败[" + error.message + "]", type: 'error'});
      });
    },
    edit() {
    },
    getSystemDictionary() {
      this.$http.get("/system/systemdictionary/list").then(result => {
        let {success,message,data} = result.data
        if (success) {
          this.systemDictionary = data;
        }
      }).catch(error => {
        this.listLoading = false;
        this.$message({message: "数据加载失败[" + error.message + "]", type: 'error'});
      })
    }
  },
  mounted() {
    this.getTableData();
  }
}

</script>

<style scoped>

</style>